package com.example.stuwork.controller;

import com.example.stuwork.common.Result;
import com.example.stuwork.pojo.Student;
import com.example.stuwork.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 学生管理控制器
 */
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 获取所有学生
     *
     * @return 学生列表
     */
    @GetMapping
    public Result<List<Student>> getAllStudents() {
        return studentService.getAllStudents();
    }

    /**
     * 根据学号获取学生信息
     *
     * @param studentId 学号
     * @return 学生信息
     */
    @GetMapping("/{studentId}")
    public Result<Student> getStudentById(@PathVariable String studentId) {
        return studentService.getStudentById(studentId);
    }

    /**
     * 添加学生
     *
     * @param student 学生信息
     * @return 结果
     */
    @PostMapping
    public Result<?> addStudent(@RequestBody Student student) {
        return studentService.addStudent(student);
    }

    /**
     * 更新学生信息
     *
     * @param student 学生信息
     * @return 结果
     */
    @PutMapping
    public Result<?> updateStudent(@RequestBody Student student) {
        // 日志输出便于调试
        System.out.println("收到更新学生请求: " + student);
        if (student.getAvatarUrl() != null) {
            System.out.println("头像URL: " + student.getAvatarUrl());
        }
        return studentService.updateStudent(student);
    }

    /**
     * 通过学号更新学生信息
     *
     * @param studentId 学号
     * @param studentData 学生部分信息
     * @return 结果
     */
    @PutMapping("/{studentId}")
    public Result<?> updateStudentById(@PathVariable String studentId, @RequestBody Map<String, Object> studentData) {
        // 日志输出便于调试
        System.out.println("收到通过ID更新学生请求: " + studentId);
        System.out.println("更新数据: " + studentData);
        
        // 创建学生对象并设置ID
        Student student = new Student();
        student.setStudentId(studentId);
        
        // 设置需要更新的字段
        if (studentData.containsKey("name")) {
            student.setName((String) studentData.get("name"));
        }
        if (studentData.containsKey("email")) {
            student.setEmail((String) studentData.get("email"));
        }
        if (studentData.containsKey("phone")) {
            student.setPhone((String) studentData.get("phone"));
        }
        if (studentData.containsKey("avatarUrl")) {
            student.setAvatarUrl((String) studentData.get("avatarUrl"));
        }
        
        return studentService.updateStudent(student);
    }

    /**
     * 更新学生头像
     *
     * @param studentId 学号
     * @param avatarUrl 头像URL
     * @return 结果
     */
    @PutMapping("/{studentId}/avatar")
    public Result<?> updateAvatar(
            @PathVariable String studentId,
            @RequestBody AvatarUpdateRequest avatarRequest) {
        if (avatarRequest == null || avatarRequest.getAvatarUrl() == null) {
            return Result.error("头像URL不能为空");
        }
        
        Student student = new Student();
        student.setStudentId(studentId);
        student.setAvatarUrl(avatarRequest.getAvatarUrl());
        
        return studentService.updateStudent(student);
    }

    /**
     * 删除学生
     *
     * @param studentId 学号
     * @return 结果
     */
    @DeleteMapping("/{studentId}")
    public Result<?> deleteStudent(@PathVariable String studentId) {
        return studentService.deleteStudent(studentId);
    }

    /**
     * 修改密码
     *
     * @param studentId    学号
     * @param oldPassword  旧密码
     * @param newPassword  新密码
     * @return 结果
     */
    @PostMapping("/change-password")
    public Result<?> changePassword(
            @RequestParam String studentId,
            @RequestParam String oldPassword,
            @RequestParam String newPassword) {
        return studentService.changePassword(studentId, oldPassword, newPassword);
    }

    /**
     * 按班级ID获取学生
     *
     * @param classId 班级ID
     * @return 学生列表
     */
    @GetMapping("/class/{classId}")
    public Result<List<Student>> getStudentsByClassId(@PathVariable Long classId) {
        return studentService.getStudentsByClassId(classId);
    }

    /**
     * 按院系编号获取学生
     *
     * @param deptNumber 院系编号
     * @return 学生列表
     */
    @GetMapping("/department/{deptNumber}")
    public Result<List<Student>> getStudentsByDeptNumber(@PathVariable String deptNumber) {
        return studentService.getStudentsByDeptNumber(deptNumber);
    }

    /**
     * 按入学年份获取学生
     *
     * @param year 入学年份
     * @return 学生列表
     */
    @GetMapping("/year/{year}")
    public Result<List<Student>> getStudentsByYear(@PathVariable String year) {
        return studentService.getStudentsByYear(year);
    }

    /**
     * 按专业编码获取学生
     *
     * @param majorCode 专业编码
     * @return 学生列表
     */
    @GetMapping("/major/{majorCode}")
    public Result<List<Student>> getStudentsByMajorCode(@PathVariable String majorCode) {
        return studentService.getStudentsByMajorCode(majorCode);
    }

    /**
     * 按课程ID获取学生
     *
     * @param courseId 课程ID
     * @return 学生列表
     */
    @GetMapping("/course/{courseId}")
    public Result<List<Student>> getStudentsByCourseId(@PathVariable Long courseId) {
        return studentService.getStudentsByCourseId(courseId);
    }

    /**
     * 搜索学生
     *
     * @param keyword 关键字
     * @return 学生列表
     */
    @GetMapping("/search")
    public Result<List<Student>> searchStudents(@RequestParam String keyword) {
        return studentService.searchStudents(keyword);
    }

    /**
     * 获取班级学生数量
     *
     * @param classId 班级ID
     * @return 学生数量
     */
    @GetMapping("/class/{classId}/count")
    public Result<Integer> getStudentCountByClassId(@PathVariable Long classId) {
        return studentService.getStudentCountByClassId(classId);
    }

    /**
     * 头像更新请求类
     */
    static class AvatarUpdateRequest {
        private String avatarUrl;

        public String getAvatarUrl() {
            return avatarUrl;
        }

        public void setAvatarUrl(String avatarUrl) {
            this.avatarUrl = avatarUrl;
        }
    }
} 